﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.BLL.DicMgr;
using Lenovo.CFI.BLL.Sys;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.Common.Sys;
using Lenovo.CFI.Common.Dic;

namespace Lenovo.CFI.Web.VP
{
    public partial class Stat1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindHelper.BindTopics(this.DdlTopic, null, null, null);

                this.GvList.Visible = false;

                this.Page.Form.DefaultButton = this.BtnStat.UniqueID;
            }
        }


        int[] all = null;
        int topicID = -1;
        Dictionary<string, string> dcOwners = null;
        protected void BtnStat_Click(object sender, EventArgs e)
        {
            this.topicID = Convert.ToInt32(this.DdlTopic.SelectedValue);

            dcOwners = new Dictionary<string, string>();
            foreach (TopicDcIp dc in new TopicBl().GetDCAndIPOwner(topicID))
            {
                if (!dc.IsIP)
                {
                    dcOwners.Add(dc.Aspect, dc.User);
                }
            }

            Dictionary<string, int[]> typesNumbers = new StatBl().TypeNumberByTopic(topicID);
            this.all = typesNumbers["all"];
            typesNumbers.Remove("all");

            this.GvList.Visible = true;
            this.GvList.DataSource = typesNumbers;
            this.GvList.DataBind();
        }

        protected void GvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)    // 数据行
            {
                KeyValuePair<string, int[]> number = (KeyValuePair<string, int[]>)e.Row.DataItem;

                ((Literal)(e.Row.FindControl("LtrType"))).Text = IdeaTypeDicMgr.GetByCode(number.Key).Title;

                ((Literal)(e.Row.FindControl("LtrNumber"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "type", number.Key, "stat", "1"), number.Value[0]);

                ((Literal)(e.Row.FindControl("LtrProduct"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "type", number.Key, "action", NextAction.PRODUCT, "stat", "1"), number.Value[1]);

                ((Literal)(e.Row.FindControl("LtrJustdoit"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "type", number.Key, "action", NextAction.JUSTDOIT, "stat", "1"), number.Value[2]);

                ((Literal)(e.Row.FindControl("LtrCproject"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "type", number.Key, "action", NextAction.CPROJECT, "stat", "1"), number.Value[3]);

                ((Literal)(e.Row.FindControl("LtrCgroup"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "type", number.Key, "action", NextAction.GROUPC, "stat", "1"), number.Value[4]);

                ((Literal)(e.Row.FindControl("LtrPool"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "type", number.Key, "action", NextAction.TBD, "stat", "1"), number.Value[5]);

                ((Literal)(e.Row.FindControl("LtrMkt"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "type", number.Key, "action", NextAction.MKT, "stat", "1"), number.Value[6]);

                ((Literal)(e.Row.FindControl("LtrNextRound"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "type", number.Key, "action", NextAction.NEXTROUND, "stat", "1"), number.Value[7]);

                ((Literal)(e.Row.FindControl("LtrOtherbu"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "type", number.Key, "action", NextAction.OTHERBU, "stat", "1"), number.Value[8]);

                ((Literal)(e.Row.FindControl("LtrDcOwner"))).Text = dcOwners[number.Key];

            }
            else if (e.Row.RowType == DataControlRowType.Footer)    // Footer
            {
                ((Literal)(e.Row.FindControl("LtrNumber"))).Text = all[0].ToString();

                ((Literal)(e.Row.FindControl("LtrProduct"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "action", NextAction.PRODUCT, "stat", "1"), all[1]);

                ((Literal)(e.Row.FindControl("LtrJustdoit"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "action", NextAction.JUSTDOIT, "stat", "1"), all[2]);

                ((Literal)(e.Row.FindControl("LtrCproject"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "action", NextAction.CPROJECT, "stat", "1"), all[3]);

                ((Literal)(e.Row.FindControl("LtrCgroup"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "action", NextAction.GROUPC, "stat", "1"), all[4]);

                ((Literal)(e.Row.FindControl("LtrPool"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "action", NextAction.TBD, "stat", "1"), all[5]);

                ((Literal)(e.Row.FindControl("LtrMkt"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "action", NextAction.MKT, "stat", "1"), all[6]);

                ((Literal)(e.Row.FindControl("LtrNextRound"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "action", NextAction.NEXTROUND, "stat", "1"), all[7]);

                ((Literal)(e.Row.FindControl("LtrOtherbu"))).Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                    UrlHelper.GenerateUrl("search", "topicid", topicID.ToString(), "action", NextAction.OTHERBU, "stat", "1"), all[8]);
            }
        }
    }
}